
//import 'package:background_fetch/background_fetch.dart';
import 'package:elevator/db/db_helper.dart';
import 'package:elevator/get/intl_keys.dart';
import 'package:elevator/model/user.dart';
import 'package:elevator/utils/utils_logger.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'all/page_all_view.dart';
import 'mine/page_mine_view.dart';
import 'page_home_logic.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
  final logic = Get.put(PageHomeLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: logic.willPop,
      child: Scaffold(
        floatingActionButton: Offstage(
          offstage: true,
          child: FloatingActionButton(
              child: const Text('', style: TextStyle(color: Colors.white)),
              onPressed: () async {
                // Get.to(MyTableComponent());
              }),
        ),
        bottomNavigationBar:  BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Colors.amber,
          unselectedItemColor: Colors.grey,
          items: [
            const BottomNavigationBarItem(
                label: 'ALL', icon: Icon(Icons.widgets)),
            BottomNavigationBarItem(
                label: IntlKeys.mine.tr, icon: Icon(Icons.person)),
          ],
          currentIndex: logic.currentIndex,
          onTap: (index) {
            if (index != logic.currentIndex) {
              logic.jumpTo(index);
              setState(() {
                logic.currentIndex = index;
              });
            }
          },
        ),
        body: PageView(
          controller: logic.pageController,
          onPageChanged: (index) => setState(() {
            logic.currentIndex = index;
          }),
          physics: const NeverScrollableScrollPhysics(),
          children: <Widget>[AllPage(), MinePage()],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    logd('lifeCycle----->$state');
    switch(state) {
      case AppLifecycleState.paused:
        // BackgroundFetch.scheduleTask(TaskConfig(
        //     taskId: 'com.lee.elevator',
        //     delay: 60 * 60 * 1000  //  毫秒
        // ));

        break;

      case AppLifecycleState.detached:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.resumed:
        // TODO: Handle this case.

        break;
      case AppLifecycleState.inactive:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.hidden:
        // TODO: Handle this case.
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }



  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    DBHelper().close();
    super.dispose();
  }
}
